package com.dragon.mmochat.message;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.battle.MmochatBattle;
import com.dragon.mmochat.model.battle.MmochatRoundStateAddRole;
import com.dragon.mmochat.model.battle.MmochatRoundStateChange;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.skill.MmochatSkill;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_3_12S extends SkymobiProtocolMessage {
	MmochatBattle battle;
	MmochatPlayer me = null;
	Integer petId = null;

	public Mmochat_3_12S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_3_12);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		out.putInt(battle.getBattleId());
		out.putUnsigned((short) battle.getTurn());

		// 回合前状态变化的角色
		out.put((byte) battle.getRoundNewStates().size());
		for (Entry<Byte, MmochatRoundStateChange> entry : battle
				.getRoundNewStates().entrySet()) {
			MmochatRoundStateChange state = entry.getValue();
			int pos = entry.getKey();
			out.put((byte) 0); // 改变状态
			out.put((byte) pos);
			out.putUnsignedInt(state.getSmeValue());
			out.putInt(state.getCurPkBlood());
			out.putInt(state.getCurPkSprit());
		}

		// 回合后状态变化的角色
		out.put((byte) (battle.getNewRoles_changeState().size()
				+ battle.getNewRoles_delRole().size() + battle
				.getNewRoles_addRole().size()));		
		for (Entry<Byte, Integer> entry : battle.getNewRoles_delRole()
				.entrySet()) {
			int pos = entry.getKey();
			out.put((byte) 1); // 删除角色
			out.put((byte) pos);
		}
		for (Entry<Byte, MmochatRoundStateAddRole> entry : battle
				.getNewRoles_addRole().entrySet()) {

			MmochatRoundStateAddRole role = entry.getValue();
			int pos = entry.getKey();
			out.put((byte) 2); // 增加角色
			out.put((byte) pos);
			out.putUnsignedShort(role.getBmpType().getValue());
			out.putPrefixedString(role.getName(), 1);
			out.putUnsignedInt(role.getSmeValue());
			out.putInt(role.getTotalBlood());
			out.putInt(role.getCurPkBlood());
			out.putInt(role.getTotalSprit());
			out.putInt(role.getCurPkSprit());
			// 技能
			if (role.getRoleType() == MmochatRoundStateAddRole.人物 && me != null
					&& me.getId() == role.getId()) {
				// 更新人物
				// 法术技能
				List<MmochatSkill> attackSkills = new ArrayList<MmochatSkill>();
				for (MmochatSkill skill : role.getSkills().values()) {
					if (skill.getType().getSkillType() == MmochatSkill.攻击技能) {
						attackSkills.add(skill);
					}
				}
				if (attackSkills.size() == 0) {
					out.put((byte) 0);
				} else {
					out.put((byte) attackSkills.size());
					// 排序
					List<MmochatSkill> orderSkill = new ArrayList<MmochatSkill>(
							attackSkills);
					Collections.sort(orderSkill);
					for (MmochatSkill skill : orderSkill) {
						out.put((byte) skill.getType().getActionType()
								.getValue());
						out.putPrefixedString(skill.getType().toString(), 1);
						out.putInt(skill.getCostSprit(true));
					}
				}
				// 障碍技能
				List<MmochatSkill> limitSkills = new ArrayList<MmochatSkill>();
				for (MmochatSkill skill : role.getSkills().values()) {
					if (skill.getType().getSkillType() == MmochatSkill.障碍技能) {
						limitSkills.add(skill);
					}
				}
				if (limitSkills.size() == 0) {
					out.put((byte) 0);
				} else {
					out.put((byte) limitSkills.size());
					// 排序
					List<MmochatSkill> orderSkill = new ArrayList<MmochatSkill>(
							limitSkills);
					Collections.sort(orderSkill);
					for (MmochatSkill skill : orderSkill) {
						out.put((byte) skill.getType().getActionType()
								.getValue());
						out.putPrefixedString(skill.getType().toString(), 1);
						out.putInt(skill.getCostSprit(true));
					}
				}
			} else if (petId == null || petId != role.getId()
					|| role.getRoleType() != MmochatRoundStateAddRole.宠物) {
				out.put((byte) 0);
				out.put((byte) 0);
			} else {
				// 更新宠物
				List<MmochatSkill> attackSkills = new ArrayList<MmochatSkill>();
				for (MmochatSkill skill : role.getSkills().values()) {
					if (skill.getType().getSkillType() == 0) {
						attackSkills.add(skill);
					}
				}
				// 法术技能,只有在召唤自己的宠物时才需要下发法术技能
				if (attackSkills.size() == 0) {
					out.put((byte) 0);
				} else {
					out.put((byte) attackSkills.size());
					// 排序
					List<MmochatSkill> orderSkill = new ArrayList<MmochatSkill>(
							attackSkills);
					Collections.sort(orderSkill);
					for (MmochatSkill skill : orderSkill) {
						out.put((byte) skill.getType().getActionType()
								.getValue());
						out.putPrefixedString(skill.getType().toString(), 1);
						out.putInt(skill.getCostSprit(true));
					}
				}
				// 障碍技能
				out.put((byte) 0);
			}
		}
		for (Entry<Byte, MmochatRoundStateChange> entry : battle
				.getNewRoles_changeState().entrySet()) {
			MmochatRoundStateChange state = entry.getValue();
			int pos = entry.getKey();
			out.put((byte) 0); // 改变状态
			out.put((byte) pos);
			out.putUnsignedInt(state.getSmeValue());
			out.putInt(state.getCurPkBlood());
			out.putInt(state.getCurPkSprit());
		}
		
		if (battle.isOver()) {
			out.put((byte) 1);
		} else {
			out.put((byte) 0);
		}
	}

	public void setBattle(MmochatBattle battle) {
		this.battle = battle;
	}

	public void setMe(MmochatPlayer me) {
		this.me = me;
	}

	public void setPetId(Integer petId) {
		this.petId = petId;
	}

}
